package com.zhoug.android.common.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @Author 35574
 * @Date 2021/7/20
 * @Description
 */
public class DataValidateHelper {
    protected static final String ERROR_MOBILE_NUMBER = "手机号码格式错误";
    protected static final String ERROR_PHONE_NUMBER = "电话号码格式错误";
    protected static final String ERROR_EMAIL = "Email格式错误";


    /**
     * 非空字段验证集合
     * key存储字段描述,eg:用户名
     * value存储字段值,eg:admin
     */
    protected HashMap<String, String> nonNullMap;
    /**
     * 存储相等验证失败
     */
    protected List<String> equalsList;
    /**
     * 其它验证失败
     */
    protected List<String> otherList;

    public DataValidateHelper() {
        nonNullMap = new HashMap<>();
        equalsList = new ArrayList<>();
        otherList = new ArrayList<>();
    }


    /**
     * 非空验证
     *
     * @param value       字段值
     * @param description 字段描述
     * @return
     */
    public DataValidateHelper nonNull(@Nullable String value, @NonNull String description) {
        nonNullMap.put(description, value);
        return this;
    }

    /**
     * 相等验证
     *
     * @param obj1        数据1
     * @param obj2        数据2
     * @param description 验证失败提示
     * @return
     */
    public DataValidateHelper equals(Object obj1, Object obj2, @NonNull String description) {
        boolean success = false;
        if (obj1 != null && obj2 != null && obj1.getClass() == obj2.getClass()) {
            if (obj1 instanceof Integer) {
                int o1 = (Integer) obj1;
                int o2 = (Integer) obj2;
                success = o1 == o2;
            } else if (obj1 instanceof Long) {
                long o1 = (Long) obj1;
                long o2 = (Long) obj2;
                success = o1 == o2;
            } else if (obj1 instanceof Double) {
                double o1 = (Double) obj1;
                double o2 = (Double) obj2;
                success = o1 == o2;
            } else if (obj1 instanceof Float) {
                float o1 = (Float) obj1;
                float o2 = (Float) obj2;
                success = o1 == o2;
            } else {
                success = obj1.toString().equals(obj2.toString());
            }
        }

        if (!success && !equalsList.contains(description)) {
            equalsList.add(description);
        } else if (success) {
            equalsList.remove(description);
        }
        return this;
    }

    /**
     * 手机号码验证
     * @param value
     * @param description
     * @return
     */
    public DataValidateHelper isMobilePhoneNumber(String value, @Nullable String description) {
        if(value==null || !PhoneUtils.isMobilePhoneCode(value)){
            otherList.add(description!=null ? description:ERROR_MOBILE_NUMBER);
        }
        return this;
    }

    /**
     * 电话号码验证
     * @param value
     * @param description
     * @return
     */
    public DataValidateHelper isPhoneNumber(String value, @Nullable String description) {
        if(value==null || !PhoneUtils.isPhoneCode(value)){
            otherList.add(description!=null ? description:ERROR_PHONE_NUMBER);
        }
        return this;
    }

    /**
     * 电话号码验证
     * @param value
     * @param description
     * @return
     */
    public DataValidateHelper isEmail(String value, @Nullable String description) {
        if(value==null || !StringUtils.isEmail(value)){
            otherList.add(description!=null ? description:ERROR_EMAIL);
        }
        return this;
    }

    /**
     * 判断集合是否为空
     * @param value
     * @param description
     * @param <T>
     * @return
     */
    public <T> DataValidateHelper nonNull(@Nullable List<T> value, @NonNull String description) {
        nonNullMap.put(description, (value == null || value.size() == 0) ? null : "nonNull");
        return this;
    }

    /**
     * 开始验证
     *
     * @return
     */
    public Result validate() {
        Result result = new Result();
        result.success = true;
        //非空验证
        StringBuilder builder = new StringBuilder();
        if (nonNullMap.size() > 0) {
            for (String description : nonNullMap.keySet()) {
                String value = nonNullMap.get(description);
                if (StringUtils.isNullOrEmpty(value)) {
                    builder.append(description);
                    builder.append("、");
                    if (result.success) {
                        result.success = false;
                    }
                }
            }
        }
        //非空验证失败
        if (!result.success) {
            int length = builder.length();
            builder.delete(length - 1, length);
            builder.append("不能为空.");
        }

        //相等验证失败
        if (equalsList.size() > 0) {
            result.success = false;
            int length = equalsList.size() - 1;
            for (int i = 0; i <= length; i++) {
                builder.append(equalsList.get(i));
                if (i < length) {
                    builder.append("、");
                } else {
                    builder.append(".");
                }
            }
        }

        if (otherList.size() > 0) {
            result.success = false;
            int length = otherList.size() - 1;
            for (int i = 0; i <= length; i++) {
                builder.append(otherList.get(i));
                if (i < length) {
                    builder.append("、");
                } else {
                    builder.append(".");
                }
            }

        }

        //验证失败
        if (!result.success) {
            result.message = builder.toString();
        }
        return result;
    }


    /**
     * 验证结果
     */
    public static class Result {
        /**
         * 验证是否通过
         */
        private boolean success;
        /**
         * 验证失败提示信息
         */
        private String message;

        public boolean isSuccess() {
            return success;
        }

        public String getMessage() {
            return message;
        }
    }

}
